package com.wx.controller;

import com.alibaba.fastjson.JSONObject;
import com.wx.bean.GroupBean;
import com.wx.service.impl.IGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/group")
public class GroupController {
    @Autowired
    IGroupService iGroupService;

    @RequestMapping("/findAll")
    public JSONObject findAll() {
        List<GroupBean> list= iGroupService.findAll();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", list);
        return jsonObject;
    }


    @RequestMapping("/groupAdd")
    public JSONObject groupAdd(@RequestBody GroupBean groupBean) {
        Date d = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String data=sdf.format(d);
        groupBean.setBuildTime(data);
        int result = iGroupService.groupAdd(groupBean);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        return jsonObject;
    }

    @RequestMapping("/findById")
    public JSONObject findById(@RequestBody GroupBean groupBean) {
        GroupBean groupBean1= iGroupService.findById(groupBean);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("groupBean", groupBean1);
        return jsonObject;
    }

    @RequestMapping("/groupUpdate")
    public JSONObject groupUpdate(@RequestBody GroupBean groupBean) {
        int result= iGroupService.groupUpdate(groupBean);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        return jsonObject;
    }

    @RequestMapping("/deleteById")
    public JSONObject deleteById(@RequestBody GroupBean groupBean) {
        int result= iGroupService.deleteById(groupBean);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        return jsonObject;
    }


}
